package com.atguigu.gmall.order.receiver;

import com.atguigu.gmall.model.enums.OrderStatus;
import com.atguigu.gmall.model.enums.PaymentStatus;
import com.atguigu.gmall.model.order.OrderInfo;
import com.atguigu.gmall.order.mapper.OrderInfoMapper;

import com.atguigu.gmall.order.service.OrderService;
import com.atguigu.gmall.pay.feign.PayFeign;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;


/**
 * 取消订单监听类
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年07月12日 20:53
 * @ClassName:OrderReceiver
 * @Description: TODO
 */
@Component
public class OrderReceiver {

    @Autowired
    private OrderService orderService;
     @Autowired
    private PayFeign payFeign;
    @RabbitListener(queues = "normal_queue")
    public void canleCancel(String msg, Channel channel, Message message){
        //获取订单号
        long orderId = Long.parseLong(msg);
        //查询订单的信息获取订单的payStatus字段的值，用于判断用户的支付渠道，从而确认用户到底从哪个取消进行订单

        //关闭交易
        Boolean aBoolean = payFeign.closePay(orderId);
        if(aBoolean){
            //取消订单
            orderService.cancleOrder(orderId);
        }
        //否则,将消息丢回队列,重新再试一次
//        channel.basicReject(1L, true);
//        if(message.getMessageProperties().getRedelivered()){
        //两次以后,还是失败,记录到数据库中区,同时取消此笔订单的关闭和回滚
        //记录到数据库中去
//        }
    }
}
